import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/other/req_other.dart';
import 'package:flutter_changdian_football/api/req/user/req_user.dart';
import 'package:flutter_changdian_football/global.dart';
import 'package:flutter_changdian_football/pages/mine/user_store.dart';
import 'package:flutter_changdian_football/routers/app_routes.dart';
import 'package:flutter_changdian_football/utils/cache_utils.dart';
import 'package:flutter_changdian_football/widget/dialog/prompt_dialog.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';

class ApplicationSettingController extends GetxController {

  @override
  void onInit() {
    super.onInit();
    getCacheFileSize();
  }

  String cacheFileSize = '0.00 B';
  String version = '1.0.0';

  /// 获取缓存文件大小
  getCacheFileSize() async {
    cacheFileSize = CacheUtils.formatFileSize(await CacheUtils.getCacheFileSize());

    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    version = packageInfo.version;

    update();
  }

  /// 清除缓存大小
  cleanCacheFileSize() {
    PromptDialog.show(
      Get.context!,
      title: '清除缓存信息确认',
      desc: '清除缓存后，会清除本地缓存，不会影响您的数据，您确认执行此操作吗',
      cancelTap: () {
        Get.back();
      },
      confirmTap: () {
        Get.back();
        cacheFileSize = '0.00 B';
        CacheUtils.clearCache();
        update();
      }
    );
  }

  /// 注销账户
  accountCancellation() {
    PromptDialog.show(
      Get.context!,
      title: '注销账户信息确认',
      desc: '注销账户将清除您账户信息包括钱包余额，您确认执行此操作吗',
      cancelTap: () {
        Get.back();
      },
      confirmTap: () async {
        var resp = await ReqOther.accountCancellation();
        if (resp.status == Status.completed) {
          if (resp.data['code'] == '200') {
            Get.until((route) => Get.currentRoute == AppRoutes.main);
            UserStore.to.logOff();
          }
        }
      }
    );
  }

  /// 退出登录
  logOut() {
    PromptDialog.show(
      Get.context!,
      title: '退出登录确认',
      desc: '您确定要退出登录吗',
      cancelTap: () {
        Get.back();
      },
      confirmTap: () async {
        var resp = await ReqUser.logout();
        if (resp.status == Status.completed) {
          if (resp.data['code'] == '200') {
            Get.until((route) => Get.currentRoute == AppRoutes.main);
            UserStore.to.logOut();
          }
        }
      }
    );
  }

}